<template>
  <!-- 编辑弹出框 -->
  <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="50%">
    <el-form ref="dataForm" :model="dataForm" label-width="100px">
      <el-form-item label="序号">
        <el-input v-model="dataForm.seq"></el-input>
      </el-form-item>
      <el-form-item label="轮播图片">
        <el-input v-model="dataForm.img"></el-input>
      </el-form-item>
      <el-form-item label="标题(可选)">
        <el-input v-model="dataForm.title"></el-input>
      </el-form-item>
      <el-form-item label="商品id">
        <el-select v-model="dataForm.product_id" filterable placeholder="请选择">
          <el-option v-for="item in categoryList" :key="item.id" :label="item.id" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" size="medium" @click="handleSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import * as HomeAPI from '@/api/home.js';
  import * as CategoryAPI from "@/api/product.js";
  export default {
    name: 'homeCarouselEdit',
    data() {
      return {
        dialogVisible: false, //是否显示
        dataForm: {
          id: 0
        },
        dataList: [], //列表数据
        categoryList: [], //分类选项数据
		query:{
			product:''
		}
      };
    },
    computed: {
      //弹框标题
      dialogTitle() {
        return !this.dataForm.id ? '新增' : '编辑';
      }
    },
    created() {
      // 获取分类信息数据，初始分类选择组件数据
      CategoryAPI.getProductList().then(result => {
        this.categoryList = result.data;
        if (this.categoryList.length > 0) {
          this.query.product_id = this.categoryList[0].id;
        }
      });
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0;
        this.dialogVisible = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields();
          if (id) {
            //如果是编辑
            HomeAPI.getHomeCarouselDetailById(id)
              .then(result => {
                this.dataForm = result.data; //看接口文档，知道data是对应的用户对象
              })
              .catch(error => {});
          } else {
            //如果是新增
            this.setDataFormToDefault();
          }
        });
      },

      //提交确认
      handleSubmit() {
        this.$refs['dataForm'].validate(valid => {
          if (valid) {
            HomeAPI.updateHomeCarousel(this.dataForm)
              .then(() => {
                this.$message.success(`保存成功`);
                this.dialogVisible = false;
                this.$emit('refreshDataList'); //重新刷新列表数据
              })
              .catch(error => {});
          }
        });
      },

      setDataFormToDefault() {
        //初始默认值
        let defaultForm = {};
        defaultForm.id = 0;
        defaultForm.img = '';
        defaultForm.title = null;
        defaultForm.product_id = null;
        defaultForm.href = null;
        defaultForm.seq = 0;
        this.dataForm = Object.assign({}, defaultForm);
      }
    }
  };
</script>

<style scoped></style>
